<?php
namespace app\admin\controller;
use app\web\model\LineShiftModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 后台首页
 * @author lx
 * @version 2017-02-09 17:00:00
 */
class IndexController extends AdminBaseController {

    protected function _initialize() {
        parent::_initialize();
    }	
	
	/**
	 * 首页
	 */	
	public function index(){
		$store_id = session('storeInfo')['id'];
		//线上订单
        $where_order['agent_id']=$store_id;
		$field=("id,order_state,total_price,pay_time");
		$to_fahuo=0;//待发货
		$to_pay=0;//待付款
		$to_wancheng=0;//已完成
		$ordersum=0;//订单总数
		$moneysum=0;//线上累计销售
		$todaymoneysum=0;//今天销售
		$weekmoneysum=0;//本周销售
		$orders=Db::name("mall_order")->where($where_order)->field($field)->cache(120)->select();
		foreach($orders as $o){
		    	$ordersum++;
		    	$o_status=$o['order_state'];
		        if($o_status==5){
		        	$to_wancheng++;
					$moneysum=$moneysum+floatval($o['total_price']);
		        }else if($o_status==3){
		        	$to_fahuo++;
		        }else if($o_status==2){
		        	$to_pay++;
		        }
				if(isToday($o['pay_time'])){
					$todaymoneysum=$todaymoneysum+floatval($o['total_price']);
					$weekmoneysum=$weekmoneysum+floatval($o['total_price']);
				}else{
					if(isToday($o['pay_time'])){
						$weekmoneysum=$weekmoneysum+floatval($o['total_price']);
					}
				}
		    }
		$this->assign("ordersum",$ordersum);
		$this->assign("to_pay",$to_pay);
		$this->assign("to_fahuo",$to_fahuo);
		$this->assign("to_wancheng",$to_wancheng);
		$this->assign('moneysum',$moneysum);
		$this->assign('todaymoneysum',$todaymoneysum);	
		$this->assign('weekmoneysum',$weekmoneysum);
		
		$totalsum=Db::name("member")->cache(60)->count('id');
		$totalsum=$totalsum?$totalsum:0;  //线上会员总数
        $this->assign('totalsum',$totalsum);
        
		$todaysum = Db::name("member")->whereTime('add_time','today')->cache(60)->count('id');
		$todaysum=$todaysum?$todaysum:0;
		$this->assign('todaysum',$todaysum);
		
        $weeksum = Db::name("member")->whereTime('add_time','week')->cache(60)->count('id');
        $weeksum=$weeksum?$weeksum:0;
        $this->assign('weeksum',$weeksum);
		
        //线上商品（已售0）
        $agent_id = session('storeInfo')['id'];
        //总数       
        $ysums = Db::name("line_goods")->field('id')->where('store_id',$agent_id)-> cache(60)->count('id');
		$sales = Db::name("line_goods")->field('id')->where('store_id',$agent_id)-> cache(60)->sum('sales');
		$this->assign('ysums',$ysums);
		$this->assign('sales',$sales);

        
		//显示最新注册的10位会员
		$where['store_id']=$store_id;
		$line_member=db("member")->order("add_time desc")->paginate(10);
		//最新的10笔订单
		$order_field = 'a.id,a.sn,a.total_price,a.agent_id,a.user_id,a.pay_time,m.id,m.nick_name';		
		$order_new=db("mall_order")->alias('a')->join('member m','a.user_id=m.id')->field($order_field)->order("pay_time desc")->paginate(10);

		return $this -> fetch('index',['member'=>$line_member,'order_new'=>$order_new]);
	}

	
	
	/**
     * 更新
     */
    public function update() {
        if (request()->isPost()) {
        	
        }else{
        	$agent = db('mall_agent')->find(session('storeInfo')['id']);
        	return $this->fetch('update', ['agent' => $agent]);
        }
    }
	
	/**
     * 清除缓存
     */
    public function clear() {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
}
